package com.example.xinlijiankangsys.controller;

import com.example.xinlijiankangsys.pojo.Post;
import com.example.xinlijiankangsys.pojo.PageResult;
import com.example.xinlijiankangsys.pojo.PostWithComments;
import com.example.xinlijiankangsys.pojo.Result;
import com.example.xinlijiankangsys.service.PostService;
import com.example.xinlijiankangsys.util.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/posts")
public class PostController {

    @Autowired
    private PostService postService;

    @PostMapping
    public Result createPost(@RequestBody Post post) {
        try {
            Post createdPost = postService.createPost(post);
            System.out.println("新建帖子");
            return ReturnResultUtil.success(createdPost);

        } catch (Exception e) {
            return ReturnResultUtil.internalServerError(e.getMessage());
        }
    }

    @GetMapping("/{postId}")
    public Result getPost(@PathVariable Long postId) {
        try {
            System.out.println(11);
            PostWithComments post = postService.getPostWithComments(postId);
            if (post == null) {
                return ReturnResultUtil.notFound("帖子不存在");
            }
            return ReturnResultUtil.success(post);
        } catch (Exception e) {
            return ReturnResultUtil.internalServerError(e.getMessage());
        }
    }

    @GetMapping
    public Result getPosts(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            PageResult<Post> result = postService.getPostsByPage(page, pageSize);

            // 创建包含所有数据的Map
            Map<String, Object> responseData = new HashMap<>();
            responseData.put("list", result.getList());
            responseData.put("total", result.getTotal());
            responseData.put("page", result.getPage());
            responseData.put("pageSize", result.getPageSize());
            responseData.put("totalPages", result.getTotalPages());

            return ReturnResultUtil.success(responseData);
        } catch (Exception e) {
            return ReturnResultUtil.internalServerError(e.getMessage());
        }
    }

    @PostMapping("/{postId}/like")
    public Result likePost(@PathVariable Long postId) {
        try {
            postService.likePost(postId);
            return ReturnResultUtil.success();
        } catch (Exception e) {
            return ReturnResultUtil.internalServerError(e.getMessage());
        }
    }

    @DeleteMapping("/{postId}")
    public Result deletePost(@PathVariable Long postId) {
        try {
            postService.deletePost(postId);
            return ReturnResultUtil.success();
        } catch (Exception e) {
            return ReturnResultUtil.internalServerError(e.getMessage());
        }
    }
}